package com.cykj.pos.controller.api;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cykj.common.annotation.Log;
import com.cykj.common.core.controller.BaseController;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.enums.BusinessType;
import com.cykj.pos.domain.AllocationRelation;
import com.cykj.pos.service.IAllocationRelationService;
import com.cykj.common.utils.poi.ExcelUtil;
import com.cykj.common.core.page.TableDataInfo;

/**
 * 划拨-机具层级关系Controller
 *
 * @author weijianbo
 * @date 2025-01-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/relation" )
public class AllocationRelationController extends BaseController {

    private final IAllocationRelationService iAllocationRelationService;

    /**
     * 查询划拨-机具层级关系列表
     */
    @PreAuthorize("@ss.hasPermi('pos:relation:list')")
    @GetMapping("/list")
    public TableDataInfo list(AllocationRelation allocationRelation) {
        startPage();
        List<AllocationRelation> list = iAllocationRelationService.queryList(allocationRelation);
        return getDataTable(list);
    }

    /**
     * 导出划拨-机具层级关系列表
     */
    @PreAuthorize("@ss.hasPermi('pos:relation:export')" )
    @Log(title = "划拨-机具层级关系" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(AllocationRelation allocationRelation) {
        List<AllocationRelation> list = iAllocationRelationService.queryList(allocationRelation);
        ExcelUtil<AllocationRelation> util = new ExcelUtil<AllocationRelation>(AllocationRelation.class);
        return util.exportExcel(list, "relation" );
    }

    /**
     * 获取划拨-机具层级关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('pos:relation:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iAllocationRelationService.getById(id));
    }

    /**
     * 新增划拨-机具层级关系
     */
    @PreAuthorize("@ss.hasPermi('pos:relation:add')" )
    @Log(title = "划拨-机具层级关系" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AllocationRelation allocationRelation) {
        return toAjax(iAllocationRelationService.save(allocationRelation) ? 1 : 0);
    }

    /**
     * 修改划拨-机具层级关系
     */
    @PreAuthorize("@ss.hasPermi('pos:relation:edit')" )
    @Log(title = "划拨-机具层级关系" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AllocationRelation allocationRelation) {
        return toAjax(iAllocationRelationService.updateById(allocationRelation) ? 1 : 0);
    }

    /**
     * 删除划拨-机具层级关系
     */
    @PreAuthorize("@ss.hasPermi('pos:relation:remove')" )
    @Log(title = "划拨-机具层级关系" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iAllocationRelationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


    //查看 我是否有设备
    @PostMapping("/check/mySn/count")
    public AjaxResult checkMySnCount(@RequestBody AllocationRelation allocationRelation){
        return iAllocationRelationService.checkMySnCount(allocationRelation);
    }


    //app 我的政策  机具总数 , 开通数, 激活率
    @PostMapping("/my/policy")
    public AjaxResult myPolicy(@RequestBody String paramJson){
        return iAllocationRelationService.myPolicy(paramJson);
    }


    //app 结算政策
    @PostMapping("/back/my/settlement/policy")
    public AjaxResult backMySettlementPolicy(@RequestBody String paramJson){
        return iAllocationRelationService.backMySettlementPolicy(paramJson);
    }

    //app 返现政策
    @PostMapping("/back/my/cashback/policy")
    public AjaxResult backMyCashbackPolicy(@RequestBody String paramJson){
        return iAllocationRelationService.backMyCashbackPolicy(paramJson);
    }

    //app 提现政策
    @PostMapping("/back/my/withdrawal/policy")
    public AjaxResult backMyWithdrawalPolicy(){
        return iAllocationRelationService.backMyWithdrawalPolicy();
    }

    //app 提现分润政策
    @PostMapping("/back/my/withdrawal/profit/policy")
    public AjaxResult backMyWithdrawalProfitPolicy(@RequestBody String paramJson){
        return iAllocationRelationService.backMyWithdrawalProfitPolicy(paramJson);
    }
}
